package com.sduept.nwld.dataserver.manager.fault;

import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.entity.FaultSourceUploadInfo;
import com.sduept.bigdata.fault.manager.FaultRecordQueryManager;
import com.sduept.cim.model.extend.LineSegment;
import com.sduept.cim.model.wires.DcLinesegment;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.fault.analysis.model.FaultUploadInfoDTO;
import com.sduept.nwld.fault.analysis.model.StationFaultUploadInfoDTO;
import com.sduept.utils.SpringContext;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


//@Service
//@LineFault
public class LineFaultUploadTransform implements IFaultUploadTransform {

//	@Autowired
//	private FaultRecordQueryManager fqm;
//	@Autowired
//	private CimManager cm ;
	
	private FaultRecordQueryManager fqm = SpringContext.getBean(FaultRecordQueryManager.class);
	private CimManager cm = SpringContext.getBean(CimManager.class);
	
	/**
	 * 将故障上送信息按照页面展示格式封装成DTO
	 * 根据List<FaultSourceUploadInfo>获取List<FaultUploadInfoDTO>
	 */
	@Override
	public List<FaultUploadInfoDTO> transform(List<FaultSourceUploadInfo> infos) {
		Map<String, Map<String, List<FaultSourceUploadInfo>>> groupMap = infos.stream().collect(Collectors.groupingBy(FaultSourceUploadInfo::getFaultId, Collectors.groupingBy(FaultSourceUploadInfo::getStationName)));
		List<FaultUploadInfoDTO> result = new ArrayList<FaultUploadInfoDTO>();
		for (String faultId : groupMap.keySet()) {
			// valueMap<变电站名称，List<FaultSourceUploadInfo>>
			Map<String, List<FaultSourceUploadInfo>> valueMap = groupMap.get(faultId);
			FaultUploadInfoDTO dto = genFaultUploadInfoDTOByFault(faultId,valueMap);
			result.add(dto);
		}
		return result;
	}
	
	/**
	 *. 获取FaultUploadInfoDTO
	 * @param fault
	 * @param valueMap
	 * @return
	 */
	private FaultUploadInfoDTO genFaultUploadInfoDTOByFault(String faultId, Map<String, List<FaultSourceUploadInfo>> valueMap) {
//		FaultRecord fault = dao.findById(faultId, FaultRecord.class);
		FaultRecord fault = fqm.findById(faultId);
		// 设置FaultUploadInfoDTO的公共属性
		FaultUploadInfoDTO dto = setCommonAttributes(fault);
		// 获取FaultUploadInfoDTO的StationFaultUploadInfoDTO
		List<StationFaultUploadInfoDTO> stationInfos = genStationFaultUploadInfoDTO(valueMap);
		dto.setStationInfo(stationInfos);
		return dto;
	}

	/**
	 * 获取List<StationFaultUploadInfoDTO>
	 * @param valueMap
	 * @return
	 */
	private List<StationFaultUploadInfoDTO> genStationFaultUploadInfoDTO(Map<String, List<FaultSourceUploadInfo>> valueMap) {
		List<StationFaultUploadInfoDTO> stationInfos = new ArrayList<StationFaultUploadInfoDTO>();
		for (String stationName : valueMap.keySet()) {
			List<FaultSourceUploadInfo> list = valueMap.get(stationName);
			StationFaultUploadInfoDTO sdto = new StationFaultUploadInfoDTO();
			sdto.setStationName(stationName);
			// 生成某个变电站的StationFaultUploadInfoDTO
			genLineStationFaultUploadInfoDTO(list,sdto);
			stationInfos.add(sdto);
		}
		return stationInfos;
	}
	
	/**
	 * 根据故障生成DTO，设置公共属性
	 * @return 
	 */
	private FaultUploadInfoDTO setCommonAttributes(FaultRecord fault) {
		FaultUploadInfoDTO dto = new FaultUploadInfoDTO();
		dto.setId(ObjectId.get().toString());
		dto.setRecordId(fault.getId());
		dto.setFaultDevice(fault.getEquipment());
		dto.setFaultTime(fault.getFaultTime());
		if("线路".equals(fault.getEtype())){
			if(StringUtils.isNotEmpty(fault.getEquipment_id())){
				LineSegment line = cm.getLineSegmentById(fault.getEquipment_id());
				if(line instanceof DcLinesegment){
					dto.setEtype("直流线路");
				}else{
					dto.setEtype("交流线路");
				}
			}
			if(StringUtils.isEmpty(fault.getFaultPhase()))
				dto.setFaultDesc(fault.getEquipment()+"故障");
			else
				dto.setFaultDesc(fault.getEquipment()+fault.getFaultPhase()+"相故障");	
		}
		return dto;
	}
	
	/**
	 * 将一个故障中一个变电站对应的多个FaultUploadInfo，封装成StationFaultUploadInfoDTO
	 * @param list
	 * @return
	 */
	private void genLineStationFaultUploadInfoDTO(List<FaultSourceUploadInfo> list,StationFaultUploadInfoDTO dto) {
		int deviceType = -1; // 设备类型：0：录波器，1：主保护， 2：断路器保护，3：非电量保护
		for (FaultSourceUploadInfo info : list) {
			deviceType = info.getDeviceType();
			if(0 == deviceType) {
				setCenterWaveUploadMethod(info, dto); // 设置集中录波上送方式
			} else if (1 == deviceType) {
				setMainProtectUploadMethod(info, dto); // 设置主保护录波和元件的上送方式
			} else if (2 == deviceType) {
				setBreakerProtectUploadMethod(info, dto); // 设置断路器保护录波和元件的上送方式
			}
		}
	}
	
	/**
	 * 设置集中录波的上送方式
	 * @param info
	 * @param dto
	 * @return
	 */
	private void setCenterWaveUploadMethod(FaultSourceUploadInfo info, StationFaultUploadInfoDTO dto) {
		if(0==info.getUploadType() && 0==info.getDeviceType()) {
			dto.setWaveInfo(info.getUploadMethod());
		}
	}
	/**
	 * 设置主保护录波和元件的上送方式
	 * @param info
	 * @param dto
	 * @return
	 */
	private void setMainProtectUploadMethod(FaultSourceUploadInfo info, StationFaultUploadInfoDTO dto) {
		// 主保护录波
		if(0==info.getUploadType() && 1==info.getDeviceType()) {
			if(info.getName().contains("主一")) {
				dto.setMain1WaveInfo(info.getUploadMethod());
			} else if (info.getName().contains("主二")) {
				dto.setMain2WaveInfo(info.getUploadMethod());
			} else if (info.getName().contains("主三")) {
				dto.setMain3WaveInfo(info.getUploadMethod());
			}
		}
		// 主保护元件
		if(1==info.getUploadType() && 1==info.getDeviceType()) {
			if(info.getName().contains("主一")) {
				dto.setMain1DatagramInfo(info.getUploadMethod());
			} else if (info.getName().contains("主二")) {
				dto.setMain2DatagramInfo(info.getUploadMethod());
			} else if (info.getName().contains("主三")) {
				dto.setMain3DatagramInfo(info.getUploadMethod());
			}
		}
	}
	/**
	 * 设置断路器保护录波和元件的上送方式
	 * @param info
	 * @param dto
	 * @return
	 */
	private void setBreakerProtectUploadMethod(FaultSourceUploadInfo info, StationFaultUploadInfoDTO dto) {
		// 断路器保护录波
		if(0==info.getUploadType() && 2==info.getDeviceType()) {
			if(StringUtils.isEmpty(dto.getBreakerProtect1Name()) || info.getName().equals(dto.getBreakerProtect1Name())) {
				dto.setBreakerProtect1Name(info.getName());
				dto.setBreakerProtect1WaveInfo(info.getUploadMethod());
			} else if(StringUtils.isEmpty(dto.getBreakerProtect2Name()) || info.getName().equals(dto.getBreakerProtect2Name())){
				dto.setBreakerProtect2Name(info.getName());
				dto.setBreakerProtect2WaveInfo(info.getUploadMethod());
			}
		}
		// 断路器保护元件
		if(1==info.getUploadType() && 2==info.getDeviceType()) {
			if(StringUtils.isEmpty(dto.getBreakerProtect1Name()) || info.getName().equals(dto.getBreakerProtect1Name())) {
				dto.setBreakerProtect1Name(info.getName());
				dto.setBreakerProtect1DatagramInfo(info.getUploadMethod());
			} else if(StringUtils.isEmpty(dto.getBreakerProtect2Name()) || info.getName().equals(dto.getBreakerProtect2Name())){
				dto.setBreakerProtect2Name(info.getName());
				dto.setBreakerProtect2DatagramInfo(info.getUploadMethod());
			}
		}
	}
	
}
